/**
  * [parseTime description]  时间格式化函数
  * @param  {[type]} time    [时间对象]
  * @param  {[type]} cFormat [格式]
  * @return {[type]} timeStr [格式化后时间]
  */

var md5 = require('./md5.js')
module.exports = {
  md5: md5.createHash,
  parseTime(time, cFormat) {
    if (arguments.length === 0) {
      return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    var date
    if (typeof time === 'object') {
      date = time
    } else {
      if (new Date(time) + '' === 'Invalid Date') {
        date = new Date(time.substr(0, 10) + 'T' + time.substr(11, 8)) // 兼容safari写法
      } else {
        date = new Date(time)
      }
    }
    const formatObj = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay()
    }
    const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
      let value = formatObj[key]
      if (key === 'a') return ['日', '一', '二', '三', '四', '五', '六'][value]
      if (result.length > 0 && value < 10) {
        value = '0' + value
      }
      return value || 0
    })
    return timeStr
  },


  /**
   * [formatTime description] 时间展示函数
   * @param  {[type]} time   [时间戳]
   * @param  {[type]} option [参数]
   * @return {[type]}        [格式化后时间]
   */
  formatTime(time, option) {
    const d = new Date(time)
    //获取当天0点的时间戳
    const etime = new Date(new Date(new Date().toLocaleDateString()).getTime()+(1000*60*60*24-1)).getTime()
    const ztime = new Date(new Date(new Date().toLocaleDateString()).getTime()).getTime()
    const wlasttime = new Date(new Date(new Date().toLocaleDateString()).getTime() + (1000 * 60 * 60 * 24 * (7 - new Date().getDay()) )).getTime()

    //获取本周一0点的时间戳
    const wtime = etime - (new Date().getDay()) * 1000 * 60 * 60 * 24
    const now = Date.now()
    const diff = (now - d) / 1000
    if (ztime < time && time < etime ) {
     return this.parseTime(time, "{h}:{i}") //Math.ceil(diff / 3600) + '小时前'
    } else if (ztime < time && time < (ztime+3600 * 24 )) {
      return '昨天'
    } else if (time > wtime && time < wlasttime) {
     return "星期"+this.parseTime(time, "{a}")
   }
    if (option) {
      return this.parseTime(time, option)
    } else {
      return d.getMonth() + 1 + '月' + d.getDate() + '日' 
    }
  },

  deepCopy (obj) {
    return JSON.parse(JSON.stringify(obj))
  },
  convertHtmlToText: function convertHtmlToText(inputText) {
    var returnText = "" + inputText;
    returnText = returnText.replace(/<\/div>/ig, '\r\n');
    returnText = returnText.replace(/<\/li>/ig, '\r\n');
    returnText = returnText.replace(/<li>/ig, '  *  ');
    returnText = returnText.replace(/<\/ul>/ig, '\r\n');
    //-- remove BR tags and replace them with line break
    returnText = returnText.replace(/<br\s*[\/]?>/gi, "\r\n");

    //-- remove P and A tags but preserve what's inside of them
    returnText = returnText.replace(/<p.*?>/gi, "\r\n");
    returnText = returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

    //-- remove all inside SCRIPT and STYLE tags
    returnText = returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText = returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText = returnText.replace(/<(?:.|\s)*?>/g, "");

    //-- get rid of more than 2 multiple line breaks:
    returnText = returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\r\n\r\n");

    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g, '');

    //-- get rid of html-encoded characters:
    returnText = returnText.replace(/ /gi, " ");
    returnText = returnText.replace(/&/gi, "&");
    returnText = returnText.replace(/"/gi, '"');
    returnText = returnText.replace(/</gi, '<');
    returnText = returnText.replace(/>/gi, '>');

    return returnText;
  },
  formatMoney (value) {
    var money, result = '', counter = 0;
    money = (value || 0).toString();
    var littlemoneyNum = money.split('.')
    money = littlemoneyNum[0]
    for (var i = money.length - 1; i >= 0; i--) {
      counter++;
      result = money.charAt(i) + result;
      if (!(counter % 3) && i != 0) {
        result = ',' + result;
      }
    }
    if (littlemoneyNum.length > 1) {
      result = result + '.' + littlemoneyNum[1]
    }
    return result
  }
}
